<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('配置名称')" prop="name">
            <el-input v-model="form.name" :placeholder="$t('配置名称')" show-word-limit maxlength="20" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('配置编码')" prop="encode">
            <el-input v-model="form.encode" :placeholder="$t('配置编码')" show-word-limit maxlength="20" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('类型')" prop="type">
            <dict-select
              v-model="form.type"
              dictionary="deal_type_label"
              :placeholder="$t('类型')"
              multiple
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('状态')" prop="status">
            <dict-select
              v-model="form.status"
              dictionary="deal_tag_status"
              :placeholder="$t('状态')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('排序')" prop="sort">
            <el-input-number v-model="form.sort" :placeholder="$t('排序')" :min="1" :max="9999" :precision="0" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="deal:tags" />
    </div>
  </div>
</template>

<script>
import { saveDealTagsConfigure, getDealTagsConfigure } from '@/api/deal/tags/dealTagsConfigure'

export default {
  data() {
    return {
      // 表单参数
      form: {},
      // 遮罩层
      loading: false,
      // 表单校验
      rules: {
        name: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        encode: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        type: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        status: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        sort: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getDicts('deal_tag_status').then(response => {
      this.statusOptions = response.data
    })
  },
  methods: {
    // 初始化数据
    init(data) {
      this.loading = true
      if (data != null && data.id !== undefined) {
        getDealTagsConfigure(data.id).then(response => {
          this.form = response.data
          this.loading = false
        })
      } else {
        this.reset()
        this.loading = false
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,

        name: undefined,

        encode: undefined,

        type: [],

        sort: undefined,

        status: 'create',

        delFlag: undefined,

        createBy: undefined,

        createTime: undefined,

        updateBy: undefined,

        updateTime: undefined

      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          saveDealTagsConfigure(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
              this.closeForm()
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      })
    }

  }

}
</script>
